/***************************************************************
Copyright ? wq Co., Ltd. 1998-2099. All rights reserved.
文件名	   : application.c
版本	   : V0.0
描述	   : CWP085 master 测试应用案例。
其他	   : 无
日志	   : 初版V0.0 2025/01/17 
***************************************************************/
/*******************************************************************************
* Include 
*******************************************************************************/ 

#include "application.h"
#include "app_Uart.h"
#include "CySysMaster.h"
#include "CySysKey.h"
#include "stdio.h"
/*******************************************************************************
* Function Prototypes
*******************************************************************************/ 

 static void User_task_process_10ms(void); 
 static void User_task_process_15ms(void);
 static void User_task_process_500ms(void);
 static void CySysTickUserCallback(void);
 static uint32_t  CySysTickUserGet(void);
 

/*******************************************************************************
* Valuse 
*******************************************************************************/ 
static uint32_t CySysCount=0;
uint32_t TaskPollPreTick[GCCOM_TASK_NUM_MAX];
const TaskAttr TaskArr[GCCOM_TASK_NUM_MAX] =
{  
        {10,      User_task_process_10ms},
        {15,       User_task_process_15ms},
        {500,      User_task_process_500ms},
        {0xFFFF,   NULL}
};

CON_CMD  CySysCMD;
SysFlag  CySysFlag;
/*******************************************************************************
* Function Name  : GCCOM_Task_Polling
* Description    : 遍历Task列表，执行不同的Task
* Input          : None
* Output         : None
* Return         : None
* Note           :该函数放在main()的while循环中
*******************************************************************************/
void GCCOM_Task_Polling( void )
{
    uint8_t i = 0;
    uint32_t tick;
    for (i = 0 ; i < GCCOM_TASK_NUM_MAX; i++)
    {
        if (TaskArr[i].mPollInterval == 0xffff)
        {
            break;
        }

        if (TaskArr[i].mPollFunc == NULL)
        {
            break;
        }
        tick = CySysTickUserGet();
        if (tick - TaskPollPreTick[i] >= TaskArr[i].mPollInterval)
        {
            TaskPollPreTick[i] = tick;
            TaskArr[i].mPollFunc();
        }
    }
}


/*******************************************************************************
* Function Name  : CySysTickInit()
* Description    : tick source init
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void CySysUserTickInit(void)
{
    CySysTickStart();// start SysTick
    CySysTickSetCallback(0, CySysTickUserCallback);//configure callback
}

/*******************************************************************************
* Function Name  : CySysTickUserCallback
* Description    : user get tick count
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
static void CySysTickUserCallback(void)
{
    CySysCount++;  
}
/*******************************************************************************
* Function Name  : CySysTickUserGet
* Description    : tick count get
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
static uint32_t  CySysTickUserGet(void)
{
    return CySysCount;  
}


/*******************************************************************************
* Function Name  :  User_task_process_10ms
* Description    : 10ms进入一次的任务
* Input          : None
* Output         : None
* Return         : None
* Note           : None
*******************************************************************************/
static void User_task_process_15ms(void)
{
    if(CySysFlag.bits.Runstate)
    {
        if( CySysFlag.bits.Runstate)
        {
            Touch_Key_Scan();
            CycSystem_Display();
        }
    }
    
}

/*******************************************************************************
* Function Name  :  User_task_process_100ms
* Description    : 100ms进入一次的任务
* Input          : None
* Output         : None
* Return         : None
* Note           : None
*******************************************************************************/
static void User_task_process_10ms(void)
{
     APP_receive_message_progress();
}

/*******************************************************************************
* Function Name  :  User_task_process_500ms
* Description    : 100ms进入一次的任务
* Input          : None
* Output         : None
* Return         : None
* Note           : None
*******************************************************************************/
static void User_task_process_500ms(void)
{
   static uint8_t runStartCount=0;
   //static uint8_t sendbuff[8]={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};
   CySysFlag.bits.Flashflag^=1;
   LED_Write(CySysFlag.bits.Flashflag);
   
    if(CySysFlag.bits.Runstate==0)
    {
        if(runStartCount++>10)
        {
            runStartCount=0;
            CySys_init_slaver();
            CySysFlag.bits.Runstate=1;
        }
    }
    //if(CySysFlag.bits.Flashflag)
    //{
//        CySysFlag.bits.RunCount++;
//        if(CySysFlag.bits.RunCount==4)
//        {
//            CySysFlag.bits.RunCount=0;
//            CySysFlag.bits.Flashflag^=1;
//            LED_Write(CySysFlag.bits.Flashflag);
//             App_UART_Send(sendbuff,8);
//        }
       
    //}
}



/*******************************************************************************
* Function Name  : CySys_init
* Description    : init
* Input          : None
* Output         : None
* Return         : None
* Note           : None
*******************************************************************************/
void CySys_init(void)
{
    
    CySysUserTickInit();
    App_Uart_init();
    I2C_Start();
    CyDelay(10);

   
    
    
}
/*******************************************************************************
* Function Name  : CySys_init_slaver
* Description    : init
* Input          : None
* Output         : None
* Return         : None
* Note           : None
*******************************************************************************/
void CySys_init_slaver(void)
{
    uint8_t arrl[]={0x10,0x00};
    WriteCommandPacket(SLAVER_ADDRESS,arrl,2,I2C_I2C_MODE_COMPLETE_XFER);
    CyDelay(1);
    WriteCommandPacket(SLAVER_ADDRESS,arrl,2,I2C_I2C_MODE_COMPLETE_XFER);
    arrl[0]=0x04;
    WriteCommandPacket(SLAVER_ADDRESS,arrl,2,I2C_I2C_MODE_COMPLETE_XFER);
    CyDelay(1);
    arrl[0]=0x05;
    WriteCommandPacket(SLAVER_ADDRESS,arrl,2,I2C_I2C_MODE_COMPLETE_XFER);
    CyDelay(1);
    arrl[0]=0x06;
    WriteCommandPacket(SLAVER_ADDRESS,arrl,2,I2C_I2C_MODE_COMPLETE_XFER);
    CyDelay(1);
    arrl[0]=0x07;
    WriteCommandPacket(SLAVER_ADDRESS,arrl,2,I2C_I2C_MODE_COMPLETE_XFER);
    CyDelay(1);
    WriteCommandPacket(SLAVER_ADDRESS,arrl,1,I2C_I2C_MODE_COMPLETE_XFER);
    CyDelay(1);
    ReadStatusPacket(SLAVER_ADDRESS,  arrl, 1, I2C_I2C_MODE_COMPLETE_XFER);
    CyDelay(1);
    arrl[0]=0x08; arrl[1]=0x08;
    WriteCommandPacket(SLAVER_ADDRESS,arrl,2,I2C_I2C_MODE_COMPLETE_XFER);
    CyDelay(1);
    arrl[0]=0x09;
    WriteCommandPacket(SLAVER_ADDRESS,arrl,1,I2C_I2C_MODE_COMPLETE_XFER);
    CyDelay(1);
    ReadStatusPacket(SLAVER_ADDRESS,  arrl, 1, I2C_I2C_MODE_COMPLETE_XFER);
    CyDelay(1);
    arrl[0]=0x04;
    WriteCommandPacket(SLAVER_ADDRESS,arrl,2,I2C_I2C_MODE_COMPLETE_XFER);
    CyDelay(1);
    arrl[0]=0x05;
    WriteCommandPacket(SLAVER_ADDRESS,arrl,2,I2C_I2C_MODE_COMPLETE_XFER);
    CyDelay(1);
    arrl[0]=0x06;
    WriteCommandPacket(SLAVER_ADDRESS,arrl,2,I2C_I2C_MODE_COMPLETE_XFER);
    CyDelay(1);
    arrl[0]=0x04;
    WriteCommandPacket(SLAVER_ADDRESS,arrl,2,I2C_I2C_MODE_COMPLETE_XFER);
    CyDelay(1);
    arrl[0]=0x05;
    WriteCommandPacket(SLAVER_ADDRESS,arrl,2,I2C_I2C_MODE_COMPLETE_XFER);
    CyDelay(1);
    arrl[0]=0x06;
    WriteCommandPacket(SLAVER_ADDRESS,arrl,2,I2C_I2C_MODE_COMPLETE_XFER);
    CyDelay(1);
    arrl[0]=0x05; arrl[1]=0x40;
    WriteCommandPacket(SLAVER_ADDRESS,arrl,2,I2C_I2C_MODE_COMPLETE_XFER);
    CySysCMD.mCMD[0]=0x01;
}

